
#include <curses.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>

#include "read_fan_temperature.h"
#include "../ec_it8733_inc/ec_it8733_dev.h"

#include "ec_it8733_rw.h"

//#define DBG_UART_PRT
//#include "dbg_prt.h"
#include "../ec_it8733_inc/dbg_prt.h"

#define ENTER 10
#define ESCAPE 27
static void init_curses()
{
        initscr();
        start_color();
        init_pair(1,COLOR_WHITE,COLOR_BLUE);
        init_pair(2,COLOR_BLUE,COLOR_WHITE);
        init_pair(3,COLOR_RED,COLOR_WHITE);
        curs_set(0);
        noecho();
        keypad(stdscr,TRUE);
}
static void draw_menubar(WINDOW *menubar)
{
        int i=0;
        int nMenuWidth=20;

        //wbkgd(menubar,COLOR_PAIR(2));

        wmove(menubar,0,nMenuWidth*i);
        waddstr(menubar,"test ttyS1");
        //wattron(menubar,COLOR_PAIR(3));
        waddstr(menubar,"(F4)");
        //wattroff(menubar,COLOR_PAIR(3));

        i++;
        wmove(menubar,0,nMenuWidth*i);
        waddstr(menubar,"test clear");
        //wattron(menubar,COLOR_PAIR(3));
        waddstr(menubar,"(F5)");
        //wattroff(menubar,COLOR_PAIR(3));


        i++;
        wmove(menubar,0,nMenuWidth*i);
        waddstr(menubar,"EXIT");
        //wattron(menubar,COLOR_PAIR(3));
        waddstr(menubar,"(F9)");
        //wattroff(menubar,COLOR_PAIR(3));

}

extern EN_RUN_STATE_TYPE en_run_state;
extern EN_KEY_VALUE_TYPE en_key_value;
void *thr_fn(void *arg);

static WINDOW *msgBoxTtyS1RwSend;
static WINDOW *msgBoxTtyS1RwRecv;
static WINDOW *msgBoxTtyS1RwStatus;

static WINDOW *messagebar;

static void disp_ttyrw_send(const char* sval)
{
        mvwprintw(msgBoxTtyS1RwSend,1,20,"%25s",sval);
        touchwin(stdscr);
        refresh();
}

static void disp_ttyrw_recv(const char* sval)
{
        mvwprintw(msgBoxTtyS1RwRecv,1,20,"%25s",sval);
        touchwin(stdscr);
        refresh();
}

static void disp_ttyrw_status(const char* sval)
{
        mvwprintw(msgBoxTtyS1RwStatus,1,20,"%25s",sval);
        touchwin(stdscr);
        refresh();
}


static void disp_sleep_msg(char* str)
{
        mvwprintw(messagebar,4,2,"%s",str);
        touchwin(stdscr);
        refresh();
}

static void create_disp_box()
{
    int nboxwidth=3;
    int nboxheight=50;
    int nboxstarty=4;
    int nboxstartx=1;
    int nboxgap=3;

    msgBoxTtyS1RwSend=subwin(stdscr,nboxwidth,nboxheight,nboxstarty,nboxstartx);
    box(msgBoxTtyS1RwSend,ACS_VLINE,ACS_HLINE);
    mvwprintw(msgBoxTtyS1RwSend,1,2,"ttyS1 send: ");


    nboxstarty+=nboxgap;
    msgBoxTtyS1RwRecv=subwin(stdscr,nboxwidth,nboxheight,nboxstarty,nboxstartx);
    box(msgBoxTtyS1RwRecv,ACS_VLINE,ACS_HLINE);
    mvwprintw(msgBoxTtyS1RwRecv,1,2,"ttyS1 receive: ");

    nboxstarty+=nboxgap;
    msgBoxTtyS1RwStatus=subwin(stdscr,nboxwidth,nboxheight,nboxstarty,nboxstartx);
    box(msgBoxTtyS1RwStatus,ACS_VLINE,ACS_HLINE);
    mvwprintw(msgBoxTtyS1RwStatus,1,2,"current status: ");


    nboxstarty+=nboxgap;
    messagebar=subwin(stdscr,10,nboxheight,nboxstarty,1);
    //messagebar=subwin(stdscr,10,79,35,1);
    box(messagebar,ACS_VLINE,ACS_HLINE);
    mvwprintw(messagebar,2,2,"msg: ");

}


int win_stdscr_ttyrw()
{
    int ch;
    WINDOW *menubar;

    init_curses();

    //bkgd(COLOR_PAIR(1));

    menubar=subwin(stdscr,2,80,0,0);

    create_disp_box();

    draw_menubar(menubar);
    //move(2,1);
    //printw("Press F1 or F2 to open the menus. ");
    //printw("ESC quits.");
    refresh();

    do
    {

        ch=getch();

        //dbg_prt( "en_run_state: %4d; ch=%d", en_run_state, ch );
        dbg_prt( "en_run_state: %4d; ch=%d", en_run_state, ch );
        mvwprintw(messagebar,2,10,"en_run_state: %4d; ch=%d", en_run_state, ch);

        
        if(ch == KEY_F(4))
        {
                char strsend[32];
                char strrecv[32];
                int nlenresv;

                strcpy(strsend,"ttyS1 test send 123456");
                memset(strrecv,0x00,sizeof(strrecv));

                disp_ttyrw_status("ttyS1 testing");

                int tty_rw_test(char* strsend, int nlensend, char* strrecv, int *nlenresv);
                int nval=tty_rw_test( strsend, sizeof(strsend), strrecv, &nlenresv);

                if( nval !=0 )
                {
                        disp_ttyrw_status("open ttys1 error ");
                }


                else
                {
                        if(strncmp(strsend,strrecv,strlen(strsend))==0)
                        {
                                disp_ttyrw_status("ttyS1 test ok!");
                        }
                        else
                        {
                                disp_ttyrw_status("ttyS1 test err!");
                        }
                }

                disp_ttyrw_send(strsend);
                disp_ttyrw_recv(strrecv);

        }

        if(ch == KEY_F(5))
        {
                disp_ttyrw_send("");
                disp_ttyrw_recv("");
                disp_ttyrw_status("");
        }

        else if (ch == KEY_F(9))
        {

                en_run_state=RUN_STATE_EXIT;
                dbg_prt( "en_run_state: %4d ", en_run_state );
                mvwprintw(messagebar,3,2,"en_run_state: %4d ", en_run_state);
        }
        
        
        touchwin(stdscr);
        //werase(messagebar);
        wrefresh(messagebar);

        refresh();


    } while (ch!=KEY_F(9));

    delwin(menubar);
    delwin(messagebar);
    endwin();
    return 0;
}


